package com.ihealthway.all.interfaces.dwihealth.vo;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import javax.xml.bind.annotation.*;
import java.beans.Transient;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Bean class for DW service plan item design
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@Table("interf_dw_serviceplan")
@XmlRootElement
@XmlType(propOrder = { "serPlanId", "serPlanName", "effectiveDateStr", "endDateStr", "serPlanItems" })

public class DWServicePlan {

    private static final Log logger = Logs.get();
    private static final SimpleDateFormat DATE_FMT = new SimpleDateFormat("yyyy-MM-dd");


    @Name
    @Column("serplan_id")
    private String serPlanId;
    @Column("serplan_name")
    private String serPlanName;
    @Column("effective_date")
    private Date effectiveDate;
    @Column("end_date")
    private Date endDate;

    @Column("create_time")
    private Date createTime;
    @Column("update_time")
    private Date updateTime;
    @Column("data_status")
    private String dataStatus;


    private String effectiveDateStr;
    private String endDateStr;

    private List<DWSerPlanItem> serPlanItems;

    @XmlElement(name = "ServPlanNo")
    public String getSerPlanId() {
        return serPlanId;
    }

    public void setSerPlanId(String serPlanId) {
        this.serPlanId = serPlanId;
    }

    @XmlElement(name = "ServPlanName")
    public String getSerPlanName() {
        return serPlanName;
    }

    public void setSerPlanName(String serPlanName) {
        this.serPlanName = serPlanName;
    }

    @XmlTransient
    public Date getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(Date effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    @XmlTransient
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    @XmlTransient
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @XmlTransient
    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @XmlTransient
    public String getDataStatus() {
        return dataStatus;
    }

    public void setDataStatus(String dataStatus) {
        this.dataStatus = dataStatus;
    }

    @Transient
    @XmlElement(name = "EffectiveDate")
    public String getEffectiveDateStr() {
        return effectiveDateStr;
    }

    public void setEffectiveDateStr(String effectiveDateStr) {
        this.effectiveDateStr = effectiveDateStr;
    }

    @Transient
    @XmlElement(name = "EndDate")
    public String getEndDateStr() {
        return endDateStr;
    }

    public void setEndDateStr(String endDateStr) {
        this.endDateStr = endDateStr;
    }

    @Transient
    @XmlElementWrapper(name = "ServItem_Records")
    @XmlElement(name = "ServItem_Record")
    public List<DWSerPlanItem> getSerPlanItems() {
        return serPlanItems;
    }

    public void setSerPlanItems(List<DWSerPlanItem> serPlanItems) {
        this.serPlanItems = serPlanItems;
    }

    public ErrorInfoElement valid() {
        ErrorInfoElement error = new ErrorInfoElement();

        error.setServPlanNo(this.serPlanId);
        if (Strings.isBlank(this.serPlanName)) {
            error.setErrorInfo("服务项目编码不能为空");
            return error;
        }

        String reg = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
        if (Strings.isBlank(this.effectiveDateStr) || !this.effectiveDateStr.matches(reg)) {
            error.setErrorInfo("服务计划生效日期不能为空或格式不对");
            return error;
        }

        if (Strings.isBlank(this.endDateStr) || !this.endDateStr.matches(reg)) {
            error.setErrorInfo("服务计划终止日不能为空或格式不对");
            return error;
        }

        if (this.serPlanItems==null || this.serPlanItems.isEmpty()) {
            error.setErrorInfo("ServItem_Records节点未找到数据");
            return error;
        }

        try {
            this.effectiveDate = DATE_FMT.parse(this.effectiveDateStr);
        } catch (ParseException e) {
            logger.error(e);
            error.setErrorInfo("服务计划生效日期格式不对");
            return error;
        }

        try {
            this.endDate = DATE_FMT.parse(this.endDateStr);
        } catch (ParseException e) {
            logger.error(e);
            error.setErrorInfo("服务计划终止日格式不对");
            return error;
        }

        this.dataStatus = "1";
        this.createTime = new Date();

        return null;
    }

    @Override
    public String toString() {
        return "DWServicePlan{" +
                "serPlanId='" + serPlanId + '\'' +
                ", serPlanName='" + serPlanName + '\'' +
                ", effectiveDate=" + effectiveDate +
                ", endDate=" + endDate +
                ", createTime=" + createTime +
                ", updateTime=" + updateTime +
                ", dataStatus='" + dataStatus + '\'' +
                '}';
    }
}
